package com.huiyuanai.servlet;

import com.alibaba.fastjson.JSON;
import com.huiyuanai.dao.Provider;
import com.huiyuanai.dao.User;
import com.huiyuanai.service.provider.ProviderService;
import com.huiyuanai.service.provider.ProviderServiceImpl;
import com.huiyuanai.util.BaseUtil;
import com.huiyuanai.util.Constans;
import com.mysql.cj.util.StringUtils;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.*;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/10/17
 */
public class ProviderServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (!StringUtils.isNullOrEmpty(method) && "query".equals(method)) {
            getProviderList(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "view".equals(method)) {
            view(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "modify".equals(method)) {
            modify(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "modifyPro".equals(method)) {
            modifyPro(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "add".equals(method)) {
            add(req, resp);
        }
        if (!StringUtils.isNullOrEmpty(method) && "delprovider".equals(method)) {
            del(req, resp);
        }
    }

    //删除数据
    private void del(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获得前端数据
        String id = req.getParameter("proid");
        //调用sevice
        ProviderServiceImpl providerService = new ProviderServiceImpl();
        int result = 0;
        //传递参数
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (Integer.parseInt(id) < 0) {
            map.put("delResult", "notexist");
        }else {
            result = providerService.delProvider(id);;
            if (result > 0) {
                map.put("delResult", "true");
            }else {
                map.put("delResult", "false");
            }
        }
        //传递json
        resp.setContentType("application/json; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(map));
        writer.close();
    }

    //新增数据
    private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取前端数据
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");

        //添加数据
        Provider provider = new Provider();
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setProDesc(proDesc);
        provider.setCreationDate(new Date());
        provider.setCreatedBy(((User)req.getSession().getAttribute(Constans.USER_SESSION)).getId());

        //service
        ProviderService providerService = new ProviderServiceImpl();
        int result = providerService.addProvider(provider);

        if (result > 0) {
            resp.sendRedirect(req.getContextPath() + "/jsp/provider.do?method=query");
        }else {
            req.getRequestDispatcher("/jsp/provider/provideradd.jsp").forward(req, resp);
        }
    }

    //更新用户数据
    private void modifyPro(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");
        Provider provider = new Provider();

        System.out.println("我是ID：" +  id);
        provider.setId(Integer.parseInt(id));
        provider.setProCode(proCode);
        provider.setProName(proName);
        provider.setProContact(proContact);
        provider.setProPhone(proPhone);
        provider.setProAddress(proAddress);
        provider.setProFax(proFax);
        provider.setProDesc(proDesc);
        provider.setModifyDate(new Date());
        provider.setModifyBy(((User)req.getSession().getAttribute(Constans.USER_SESSION)).getId());

        ProviderService providerService = new ProviderServiceImpl();
        int result = providerService.upDataProvider(provider);

        if(result > 0){
            resp.sendRedirect(req.getContextPath()+"/jsp/provider.do?method=query");
        }else {
            req.getRequestDispatcher("/jsp/provider/providermodify.jsp").forward(req, resp);
        }

    }

    //更新用户显示数据
    private void modify(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("proid");
        ProviderService providerService = new ProviderServiceImpl();
        List<Provider> provider = providerService.getProvider(null, null, id, 0, 100);
        req.setAttribute("provider", provider.get(0));
        req.getRequestDispatcher("/jsp/provider/providermodify.jsp").forward(req, resp);
    }

    //详细信息
    private void view(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        ProviderService providerService = new ProviderServiceImpl();
        String id = req.getParameter("proid");
        List<Provider> provider = providerService.getProvider(null, null, id, 0, 100);
        req.setAttribute("provider",provider.get(0));
        req.getRequestDispatcher("/jsp/provider/providerview.jsp").forward(req, resp);
    }

    //获取用户数据
    private void getProviderList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取前端数据
        String proCode = req.getParameter("queryProCode");
        String proName = req.getParameter("queryProName");

        //获取servlet
        ProviderService providerService = new ProviderServiceImpl();
        List<Provider> providerList = providerService.getProvider(proCode, proName, null, 0, 100);
        req.setAttribute("providerList",providerList);
        req.setAttribute("queryProCode", proCode);
        req.setAttribute("queryProName", proName);
        req.getRequestDispatcher("/jsp/provider/providerlist.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


}
